import { createRouter, createWebHashHistory} from 'vue-router'
import MyHome from './views/MyHome.vue'

const router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            name:"home",
            component:MyHome
        },
        {
            path:"/login",
            name:"login",
            component:() => import("./views/MyLogin.vue")
        },
        {
            path:"/imgs",
            name:"imgs",
            component:() => import("./views/imgs/MyImgs.vue")
        },
        {
            path:"/blogs",
            name:"blogs",
            component:() => import("./views/blogs/MyBlog.vue"),
            redirect:'/blogs/list',
            children:[
                {
                    path:"/blogs/add",
                    name:"addBlog",
                    component:() => import("./views/blogs/AddBlog.vue")
                },
                {
                    path:"/blogs/markdown",
                    name:"markdown",
                    component:() => import("./views/blogs/MyMarkdown.vue")
                },
                {
                    path:"/blogs/list",
                    name:"blogList",
                    component:() => import("./views/blogs/BlogList.vue")
                },
                {
                    path:"/blogs/:id",
                    name:"blogDetail",
                    component:() => import("./views/blogs/BlogDetail.vue")
                }
            ]
        }
    ]
})

router.beforeEach(async (to,from,next) => {
    let token = localStorage.getItem('token');
    if(to.path === '/login' || token){
        next();
    }else{
        next('/login');
    }
})
export default router;